/*
 * @Author: DUST
 * @Date: 2023-05-16 14:40:15
 * @LastEditors: DUST
 * @LastEditTime: 2023-05-19 11:45:59
 * @FilePath: \vite-project\src\views\admin\noteManage\index.ts
 * @Description: 
 * undefined
 */
import BaseTable from "@/class/table";
import { Ref, reactive, ref } from "vue";
import { notePage, addNote, editNote, delNote, tagsLIst } from "@/api";
import { Ajax, IButton } from "@/interface";
import timeFormat from "@/utils/time";
import { modalType } from "@/interface/enum";
import { message } from "ant-design-vue";
import { AxiosResponse } from "axios";

export interface tableItem {
  index: number;
  id: number;
}
interface conformObj {
  title: string;
  description: string;
  content: string;
}
interface tagsItem {
  name: string;
  color?: string;
  id: number;
  powers: number;
  createTime: Date;
  upTime: Date;
}

class NoteManage extends BaseTable<tableItem> {
  /**
   * @description: 标签列表
   */
  private _tagList = ref<Array<tagsItem>>([]);
  public get tagList(): Ref<Array<tagsItem>> {
    return this._tagList;
  }
  /**
   * @description: 表格columns
   */
  public columns = reactive([
    {
      title: "序号",
      customRender: ({ index }: { index: number }) => index + 1,
      fixed: "left",
      width: 60
    },
    {
      title: "标题",
      dataIndex: "title",
      filterOperator: "like",
      filterIconIndex: "search",
      customFilterDropdown: true,
      width: 400,
      ellipsis: true
    },
    {
      title: "作者",
      dataIndex: "author",
      width: 200,
    },
    {
      title: "简述",
      dataIndex: "description",
      width: 400,
      ellipsis: true
    },
    {
      title: "创建时间",
      dataIndex: "createTime",
      customRender: ({ text }: { text: number }) : string => timeFormat(text, "YYYY-MM-DD HH:mm:ss") 
    },
    {
      title: "修改时间",
      dataIndex: "upTime",
      customRender: ({ text }: { text: number }) : string => timeFormat(text, "YYYY-MM-DD HH:mm:ss") 
    },
    {
      title: "操作",
      action: "operation",
      width: 110,
      isCheck: true
    }
  ]);

  /**
   * @description: 新增、编辑景区弹窗控制器
   */
  private _visible = ref<boolean>(false);
  public get visible(): Ref<boolean> {
    return this._visible;
  }
  /**
   * @description: loading
   */
  private _loading = ref<boolean>(false);
  public get loading(): Ref<boolean> {
    return this._loading;
  }
  /**
   * @description: editData
   */
  private _editData = ref<tableItem | undefined>();
  public get editData(): Ref<tableItem | undefined> {
    return this._editData;
  }
  /**
   * @description: 新增、编辑景区弹窗控制器
   */
  private _type = ref<modalType>(modalType.ADD);
  public get type(): Ref<modalType> {
    return this._type;
  }
  /**
   * @description: 显示新增弹窗
   */
  public showAdd = () => {
    this._visible.value = true;
    this._type.value = modalType.ADD;
  };
  /**
   * @description: 显示编辑弹窗
   */
  public showEdit = (record: tableItem) => {
    this._visible.value = true;
    this._type.value = modalType.EDIT;
    this._editData.value = record;
  };

  /**
   * @description: 按钮列表
   */
  private _buttonList = ref<Array<IButton>>([
    {
      name: "新增笔记",
      type: "primary",
      func: this.showAdd
    }
  ]);
  public get buttonList(): Ref<Array<IButton>> {
    return this._buttonList;
  }

  /**
   * @description: 子类构造函数
   */
  constructor() {
    super({ AJAXFunc: notePage });
    this.getTagList();
  }
  /**
   * @description: 获取随机十六进制颜色
   */
  public color16 = () => {
    //十六进制颜色随机
    const r = Math.floor(Math.random() * 200);
    const g = Math.floor(Math.random() * 200);
    const b = Math.floor(Math.random() * 200);
    const color = `#${r.toString(16).padStart(2, "0")}${g
      .toString(16)
      .padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
    // console.log(color);
    return color;
  };
  /**
   * @description: 获取tags列表
   */
  public getTagList = () => {
    tagsLIst().then((res: AxiosResponse<Ajax<Array<tagsItem>>>) => {
      console.log(res);
      res.data.data.filter((val: tagsItem) => {
        val.color = this.color16();
      });
      this._tagList.value = res.data.data;
    });
  };
  /**
   * @description: 新增记录
   * @param {conformObj} obj
   */  
  public addFunc = (obj: conformObj) => {
    addNote(obj).then(() => {
      message.success("新增成功");
      this._visible.value = false;
      this.getData();
    }).finally(() => {
      this._loading.value = false;
    })
  }
  /**
   * @description: 编辑记录
   * @param {conformObj} obj
   */  
  public editFunc = (obj: conformObj) => {
    editNote(obj).then(() => {
      message.success("编辑成功");
      this._visible.value = false;
      this.getData();
    }).finally(() => {
      this._loading.value = false;
    })
  }

  /**
   * @description: 确认提交
   * @param {object} param1
   */
  public doOk = ({type, obj}: { type: modalType; obj: conformObj }) => {
    if(type == modalType.ADD) this.addFunc(obj);
    else if(type == modalType.EDIT) this.editFunc(obj);
  }
  /**
   * @description: 删除记录-软
   * @param {object} record:需要删除的记录
   */  
  public delNote = (record: tableItem) => {
    delNote({id: record.id}).then(() => {
      message.success("删除成功");
      this.getData();
    })
  }
}

export default NoteManage;